﻿using System.Linq;
using System.Web.Http;
using System.Web.Http.Controllers;
using Common.Logging;
using OutDbContext;
using User.Api.Dto.Education;

namespace User.Api.Controllers
{
    public class EducationController : ApiController
    {
        private static readonly ILog Logger = LogManager.GetLogger(typeof(EducationController));
        private GdsfybjyWeiXinEntities _ctx;

        protected override void Initialize(HttpControllerContext controllerContext)
        {
            _ctx = new GdsfybjyWeiXinEntities();
            base.Initialize(controllerContext);
        }

        protected override void Dispose(bool disposing)
        {
            _ctx.Dispose();
            base.Dispose(disposing);
        }

        /// <summary>
        /// 获取宣教列表
        /// </summary>
        /// <param name="educationType">宣教类型</param>
        /// <returns>宣教列表</returns>
        [HttpGet]
        [ActionName("GetEducationLists")]
        public GetEducationListResponse GetEducationLists(int educationType = 0)
        {

            var educations = 
                _ctx.EducationKnowlgs.Where(x => x.TypeId == educationType && x.RecStatus == 1)
                .Select(x =>
                    new GetEducationListResponse.Education
                    {
                        Id = x.Id,
                        Title = x.Title
                    }).ToList();
            return new GetEducationListResponse()
            {
                ResultCode = "0",
                ResultMsg = "",
                EduKlgs = educations
            };
        }

        /// <summary>
        /// 获取宣教明细
        /// </summary>
        /// <param name="educationId">宣教Id</param>
        /// <returns>宣教明细</returns>
        [HttpGet]
        [ActionName("GetEducationKnowlgDetail")]
        public GetEducationKnowlgDetailResponse GetEducationKnowlgDetail(int educationId)
        {
            var education = _ctx.EducationKnowlgs.FirstOrDefault(x => x.Id == educationId && x.RecStatus == 1);
            if (education == null)
            {
                return new GetEducationKnowlgDetailResponse()
                {
                    ResultCode = "1",
                    ResultMsg = "记录不存在"
                };
            }
            return new GetEducationKnowlgDetailResponse()
            {
                ResultCode = "0",
                ResultMsg = "",
                TypeId = education.TypeId,
                Title = education.Title,
                Content = education.Content,
                Sort = education.Sort,
            };
        }
    }
}
